<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/assets/layui/css/layui.css">
</head>
<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>修改密码</legend>
      </fieldset>
       
      <form class="layui-form" action="" lay-filter="example">
        <div class="layui-form-item">
          <label class="layui-form-label">请输入旧密码</label>
          <div id="box"></div>
          <div class="layui-input-block">
               
            <input type="text" name="password" id="password" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">请输入新密码</label>
          <div class="layui-input-block">
            <input type="password" name="Newpassword" id="Newpassword" placeholder="请输入密码" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">再次确认新密码</label>
            <div class="layui-input-block">
              <input type="password" name="Npassword" id="Npassword" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
          </div>
    
  
 
        
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" id="btn" lay-filter="demo1">立即提交</button>
          </div>
        </div>
      </form>
</body>
<script src="/assets/layui/layui.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/js-cookie/3.0.1/js.cookie.min.js"></script>
<script>
    let {id} =JSON.parse(Cookies.get('userInfo'))
    console.log(id);
    $('#password').blur(function(){
        let  userVal = $('#password').val();
        $.ajax({
            type:'post',
            url:`/isoldPassword/${id}`,
            data:{password:userVal}
        }).then(res=>{
            const {code,message} = res;
            if(code === 0){
                $('#box').text(message)
            }else{
                $('#box').text(message)
            }
        })
    })
    
    $('#btn').click(function(){
        let Newpassword = $('#Newpassword').val()
        let Npassword = $('#Npassword').val()
        if(Newpassword === Npassword){
            $.ajax({
                type:'post',
                url:`/updpassword/${id}`,
                data:{pass:Npassword}
            }).then(res=>{
                const {code,message} = res;
                if(code === 0 ){
                    parent.location.href = '/login'
                }else{
                    alert(message)
                }
            })
        }
    })

</script>
</html>